package com.itheima.test.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.QueryPageBean;
import com.itheima.bean.Result;

import com.itheima.test.entity.User;
import com.itheima.test.mapper.UserMapper;
import com.itheima.test.service.UserService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    /**
     * @description:登出
     * @author: Java
     * @date: 2021/3/31 11:58
     * @param: req
     * @return: com.itheima.bean.Result
     */
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest req) throws IOException {
        System.out.println("进入UserController中的logout");
        //1.清空session
        req.getSession().invalidate();
        //告诉成功还是失败
//        JsonUtils.printResult(resp, new Result(true, "退出成功"));
        return new Result(true, "退出成功");
    }

    /**
     * @description:登录
     * @author: Java
     * @date: 2021/3/31 11:57
     * @param: user
     * @return: com.itheima.bean.Result
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        // 1.创建查询条件构建器
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        // 2.获取用户信息
        User login = userMapper.selectOne(wrapper);
        //3.返回信息
        if (login != null) return new Result(true, "登录成功", login);
        return new Result(false, "登录失败");
    }

    /**
     * @description:新增用户
     * @author: Java
     * @date: 2021/3/24 14:31
     * @param: request
     * @param: response
     * @return: void
     */
    @PostMapping("add")
    public Result add(int type, @RequestBody User user) {
        try {
            //1.创建日期
            user.setCreateDate(DateUtils.parseDate2String(new Date()));
            //2.设置权限，0是未启用
            user.setStatus(0);
            //3.设置默认密码，1
            if (user.getPassword() == null) user.setPassword("1");
            //4.设置角色
            user.setRole(type);
            //5.添加
            userService.addUser(user);
            //6.响应
            return new Result(true, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增失败");
        }


    }

    /**
     * @description:更新学生用户
     * @author: Java
     * @date: 2021/3/24 15:36
     * @param: user
     * @return: com.itheima.bean.Result
     */
    @PostMapping("update")
    public Result update(@RequestBody User user) {
        try {
            System.out.println("...............改.................");
            //2.直接更新
            userMapper.updateById(user);
            //3.响应
            return new Result(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }
    }

    /**
     * @description:删除用户
     * @author: Java
     * @date: 2021/3/24 15:02
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("delete")
    public Result delete(int id) throws IOException {
        System.out.println("UserController里delete执行...id=" + id);
        //不讲道理，直接删
        userMapper.deleteById(id);
        return new Result(true, "删除成功");
    }

    /**
     * @description:条件查询学生用户
     * @author: Java
     * @date: 2021/3/24 15:36
     * @param:
     * @return: com.itheima.bean.Result
     */
    @GetMapping("/findAll")
    public Result findAll() {
        //1.创建查询条件构建器
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        //2.设置条件
        wrapper.eq(User::getRole, 0);
        //3.查询
        List<Map<String, Object>> mapList = userMapper.selectMaps(wrapper);
        mapList.forEach(System.out::println);
        //4.返回
        if (mapList != null && mapList.size() > 0) return new Result(true, "查询成功", mapList);
        return new Result(false, "查询失败");
    }

    /**
     * @description:分页查询学生用户
     * @author: Java
     * @date: 2021/3/24 16:09
     * @param: queryPageBean
     * @return: com.itheima.bean.Result
     */
    @RequestMapping("findPage")
    public Result findPage(int type, @RequestBody QueryPageBean queryPageBean) {
        //1. 构建分页对象
        Page<User> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        try {
            if (type == 0) {
                //2.调用业务查询
                IPage<User> pageResult = userMapper.selectByPageStu(page);
                //3.封装到Result返回
                return new Result(true, "查询学生成功", pageResult);
            } else if (type == 1) {
                //2.调用业务查询
                IPage<User> pageResult = userMapper.selectByPageTea(page);
                //3.封装到Result返回
                return new Result(true, "查询教师成功", pageResult);
            }
            return new Result(false, "失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

    /**
     * @description:查看个人信息弹窗
     * @author: Java
     * @date: 2021/3/26 17:46
     * @param: username
     * @return: com.itheima.bean.Result
     */
    @RequestMapping("findByUsername")
    public Result findByUsername(String username) {
        try {
            System.out.println("username=" + username);
            //1.创建查询条件构建器
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            //2.设置条件
            wrapper.eq(User::getRole, 0)
                    .eq(User::getUsername, username);
            //3.查询
            List<Map<String, Object>> mapList = userMapper.selectMaps(wrapper);
            mapList.forEach(System.out::println);
            //4.返回
            if (mapList != null && mapList.size() > 0) return new Result(true, "查询成功", mapList);
            return new Result(false, "查无此人");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

    /**
     * @description:启用账号
     * @author: Java
     * @date: 2021/3/27 14:49
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("EnableStatus")
    public Result EnableStatus(int id) {
        try {
            User user = new User();
            user.setId(id);
            user.setStatus(1);
            //2.直接更新
            userMapper.updateById(user);
            //3.响应
            return new Result(true, "启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "启用失败");
        }
    }

    /**
     * @description:禁用账号
     * @author: Java
     * @date: 2021/3/27 14:49
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("DisableStatus")
    public Result DisableStatus(int id) {
        try {
            User user = new User();
            user.setId(id);
            user.setStatus(0);
            //2.直接更新
            userMapper.updateById(user);
            //3.响应
            return new Result(true, "禁用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "禁用失败");
        }
    }

    /**
     * @description:升级权限
     * @author: Java
     * @date: 2021/3/27 15:39
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("Uproleid")
    public Result Uproleid(int id) {
        try {
            User user = new User();
            user.setId(id);
            user.setRole(2);
            //2.直接更新
            userMapper.updateById(user);
            //3.响应
            return new Result(true, "启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "启用失败");
        }
    }

    /**
     * @description:降级权限
     * @author: Java
     * @date: 2021/3/27 15:41
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("Downroleid")
    public Result Downroleid(int id) {
        try {
            User user = new User();
            user.setId(id);
            user.setRole(1);
            //2.直接更新
            userMapper.updateById(user);
            //3.响应
            return new Result(true, "启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "启用失败");
        }
    }


}
